// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { Time, TimePickerBase } from "../common/time-picker-base.slint";
import { VerticalBox } from "../common/layout.slint";
import { IconButton, TextButton } from "./button.slint";
import { MaterialPalette, MaterialFontSettings, Icons } from "styling.slint";

export { Time }


export component TimePickerPopup inherits PopupWindow {
    in property <bool> use-24-hour-format <=> base.use-24-hour-format;
    in property <string> title: "Select time";
    in property <Time> time <=> base.time;

    callback canceled();
    callback accepted(/* current-time */ Time);

    width: dialog.width;
    height: dialog.height;

    close-policy: PopupClosePolicy.no-auto-close;

    background-layer := Rectangle {
        width: dialog.width;
        height: dialog.height;
        background: MaterialPalette.surface-container-high;
        border-radius: 28px;
    }

    dialog := Dialog {
        padding: 8px;

        base := TimePickerBase {
            title: root.title;
            style: {
                foreground: MaterialPalette.foreground,
                vertical-spacing: 8px,
                horizontal-spacing: 4px,
                clock-style: {
                    background: MaterialPalette.surface-container-highest,
                    foreground: MaterialPalette.accent-background,
                    time-selector-style: {
                        foreground: MaterialPalette.foreground,
                        foreground-selected: MaterialPalette.accent-foreground,
                        font-size: MaterialFontSettings.body-large.font-size,
                        font-weight: MaterialFontSettings.body-large.font-weight
                    }
                },
                input-style: {
                    background: MaterialPalette.surface-container-highest,
                    background-selected: MaterialPalette.accent-container,
                    foreground: MaterialPalette.foreground,
                    foreground-selected: MaterialPalette.foreground,
                    border-radius: 8px,
                    font-size: 57 * 0.0625rem,
                    font-weight: 400
                },
                period-selector-style: {
                    border-radius: 8px,
                    border-width: 1px,
                    border-brush: MaterialPalette.border,
                    item-style: {
                        font-size: MaterialFontSettings.body-large.font-size,
                        font-weight: MaterialFontSettings.body-large.font-weight,
                        foreground: MaterialPalette.foreground,
                        background-selected: MaterialPalette.tertiary-container,
                        foreground-selected: MaterialPalette.on-tertiary-container
                    }
                },
                title-style: {
                    font-size: MaterialFontSettings.label-medium.font-size,
                    font-weight: MaterialFontSettings.label-medium.font-weight,
                    foreground: MaterialPalette.foreground,
                },
            };
        }

        IconButton {
            icon: base.selection-mode ? Icons.keyboard : Icons.clock;
            accessible-label: "Toggle input picker";
            dialog-button-role: action;

            clicked => {
                base.selection-mode = !base.selection-mode;
            }
        }

        TextButton {
            text: @tr("Cancel");
            dialog-button-role: reject;

            clicked => {
                root.close();
                root.canceled();
            }
        }

        TextButton {
            text: @tr("OK");
            enabled: base.ok-enabled();
            dialog-button-role: accept;

            clicked => {
                root.close();
                root.accepted(base.get-current-time());
            }
        }
    }
}
